package com.angel.resourceBundle.propertyValue.impl;

import javax.persistence.Column;
import javax.persistence.Entity;

import com.angel.architecture.persistence.base.PersistentObject;
import com.angel.resourceBundle.propertyValue.PropertyValue;


/**
 *
 * @author William
 * @since 10/Nov/2007
 */
@Entity
public class SimplePropertyValue extends PersistentObject implements Comparable<Object>, PropertyValue{

	private static final long serialVersionUID = 6767872474095550850L;
	private Long counter;

	@Column(nullable = false, length = 60, updatable = false, unique = true)
	private String extendedKey;

	private String languageName;

	@Column(nullable = false, length = 1000)
	private String label;

	public SimplePropertyValue(){
		super();
		this.counter = new Long(0);
	}

	/**
	 * @return the counter
	 */
	public Long getCounter() {
		return counter;
	}

	/**
	 * @param counter the counter to set
	 */
	public void setCounter(Long counter) {
		this.counter = counter;
	}

	/** Increment the counter in a unit.
	 *
	 * @param nothing.
	 * @return nothing.
	 */
	protected void incrementCounter(){
		this.setCounter(this.getCounter() + 1 );
	}

	/** Increment the counter in "unit" units.
	 *
	 * @param unit.
	 * @return nothing.
	 */
	protected void incrementCounterIn(int unit){
		this.setCounter(this.getCounter() + unit );
	}

	/** Return true if the propertyValue is equals to this object.
	 * Otherwise return false. Two propertyValues are equals if
	 * their counter are equals.
	 *
	 * @param propertyValue
	 * @return boolean.
	 */
	@Override
	public boolean equals(Object obj){
		SimplePropertyValue propertyValue = (SimplePropertyValue) obj;
		return this.getCounter() == propertyValue.getCounter();
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object object) {
		SimplePropertyValue propertyValue = (SimplePropertyValue) object;
		int value;
		if(this.equals(propertyValue)){
			value = 0;
		} else {
			if(this.isGreaterThan(propertyValue)){
				value = -1;
			} else {
				value = 1;
			}
		}
		return value;
	}

	/** Return true if the param propertyValue isn't smaller than this object.
	 * Otherwise return false.
	 *
	 * @see isSmallerThan
	 * @param propertyValue
	 * @return boolean
	 */
	public boolean isGreaterThan(SimplePropertyValue propertyValue){
		return !this.isSmallerThan(propertyValue);
	}

	/** Return true if this propertyValue is smoller than the param.
	 * A propertyValue is smaller than other, if his counter is smaller than the other.
	 * Otherwise return false.
	 *
	 * @param propertyValue
	 * @return boolean
	 */
	public boolean isSmallerThan(SimplePropertyValue propertyValue){
		return this.getCounter()< propertyValue.getCounter();
	}

	/**
	 * @return the extendedKey
	 */
	public String getExtendedKey() {
		return extendedKey;
	}

	/**
	 * @param extendedKey the extendedKey to set
	 */
	public void setExtendedKey(String extendedKey) {
		this.extendedKey = extendedKey;
	}

	/**
	 * @return the label
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * @param label the label to set
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	public String getText() {
		return this.label;
	}

	/**
	 * @param internationalizerName the internationalizerName to set
	 */
	public void setLanguageName(String languageName) {
		this.languageName = languageName;
	}

	/**
	 * @return the internationalizerName
	 */
	public String getLanguageName() {
		return languageName;
	}

	public Boolean isForLanguage(String languageName) {
		return this.getLanguageName() != null && this.getLanguageName().equalsIgnoreCase(languageName);
	}

	public String getKey() {
		int indexBeginLanguaje = this.getExtendedKey().indexOf(this.getLanguageName().toUpperCase());
		String key = this.getExtendedKey().substring(indexBeginLanguaje);
		return key.toUpperCase();
	}

	public String getValue() {
		return this.getLabel();
	}

	public Boolean isForKey(Object key) {
		return this.getKey().equalsIgnoreCase(key.toString());
	}
}
